/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Video Controller
* Version: 0.2
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["video"] = new Object();
myPlugins["video"]["name"] = getLocalizedString2("mod_sysm_plugin_vid_name");
myPlugins["video"]["onlyonce"] = true;
myPlugins["video"]["newObject"] = function (NewSettings) { return new plugin_video_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_video_sensor(NewSettings) {
	// Required variables
	this.plugin = "video";
	this.settings = NewSettings;
	this.edit = plugin_video_edit;
	this.ReDraw = plugin_video_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;


	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.updspeed) this.settings.updspeed = 300;


	// The Sensor
	this.TextLine = new Array();


	// Update timer
	var obj = this;
	this.timer = new Timer();
	this.timer.interval = this.settings.updspeed;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_video_update.call(obj); };
	plugin_video_update.call(obj);


	// Calculating sensore height (required!)
	// Done with the update call!
	return true;


// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_video_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_updatespeed_label");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(180,300,600,900);
		Drive_Edit_DialogFields[FieldID].option = new Array();
		for(var i = 0; i < Drive_Edit_DialogFields[FieldID].optionValue.length ; i++ )
			Drive_Edit_DialogFields[FieldID].option[i] = getLocalizedString2("mod_sysm_plugin_updatespeed_" + Drive_Edit_DialogFields[FieldID].optionValue[i]);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.updspeed;


		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];

			if ( this.settings.updspeed != Drive_Edit_DialogResults[1] ) {
				this.settings.updspeed = Drive_Edit_DialogResults[1];
				this.timer.interval = this.settings.updspeed;
			}

			plugin_video_update.call(this);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}

	function plugin_video_update() {
		var k = -1;
		if(system.platform=="windows") {
			var tmpVideoController;
			if(VBSEnabled) tmpVideoController = runCommand('cscript.exe //nologo "' + mod_sysmonitor_path_video + '"').split("\n");
			else tmpVideoController = runCommand('"' + mod_sysmonitor_WMIExecQuery + '" "DeviceID" "1" "SELECT * FROM Win32_VideoController WHERE CurrentHorizontalResolution > 0"').split("\n");
			for(var i = 0;i < tmpVideoController.length;i++) {
				if(this.TextLine[i]==undefined) {
					this.TextLine[i] = new class_TextLine(this.frame,mod_sysmonitor_size * i,"");
				} else {
					this.TextLine[i].label.visible = true;
					this.TextLine[i].data.visible = true;
				}
	
				this.TextLine[i].label.data = getLocalizedString2("mod_sysm_plugin_vid_label") + "#" + (i+1);
	
				this.TextLine[i].data.data = wmi.Get( 'Win32_VideoController.DeviceID="' + tmpVideoController[i] + '"' ).Properties_.Item( "CurrentHorizontalResolution" ).Value;
				this.TextLine[i].data.data += " * " + wmi.Get( 'Win32_VideoController.DeviceID="' + tmpVideoController[i] + '"' ).Properties_.Item( "CurrentVerticalResolution" ).Value;
				this.TextLine[i].data.data += " @ " + wmi.Get( 'Win32_VideoController.DeviceID="' + tmpVideoController[i] + '"' ).Properties_.Item( "CurrentBitsPerPixel" ).Value + "bit ";
				this.TextLine[i].data.data += " (" + wmi.Get( 'Win32_VideoController.DeviceID="' + tmpVideoController[i] + '"' ).Properties_.Item( "CurrentRefreshRate" ).Value + "Hz)";

				this.TextLine[i].label.color = this.settings.color;
				this.TextLine[i].data.color = this.settings.color;
			}
			k = tmpVideoController.length - 1;
		} else if(system.platform=="macintosh") {
			var tmpPList = parse_plist(runCommand("system_profiler -xml SPDisplaysDataType"));
			for(i=0;i<tmpPList[0]._items.length;i++) {
				for(j=0;j<tmpPList[0]._items[i].spdisplays_ndrvs.length;j++) {
					if(tmpPList[0]._items[i].spdisplays_ndrvs[j].spdisplays_resolution!=undefined) {
						k++;
						if(this.TextLine[k]==undefined) {
							this.TextLine[k] = new class_TextLine(this.frame,mod_sysmonitor_size * k,"");
						} else {
							this.TextLine[i].label.visible = true;
							this.TextLine[i].data.visible = true;
						}
						this.TextLine[i].label.data = getLocalizedString2("mod_sysm_plugin_vid_label") + "#" + (k+1);
						if(tmpPList[0]._items[i].spdisplays_ndrvs[j].spdisplays_depth == "CGSThirtytwoBitColor")
							this.TextLine[i].data.data = tmpPList[0]._items[i].spdisplays_ndrvs[j].spdisplays_resolution + " @ 32bit";
						else
							this.TextLine[i].data.data = tmpPList[0]._items[i].spdisplays_ndrvs[j].spdisplays_resolution + " @ ??bit";
					}
				}
			}

		}

		if( this.frame.height != this.TextLine[k].data.vOffset + (this.TextLine[k].data.size * 1/3) ) {
			this.frame.height = this.TextLine[k].data.vOffset + (this.TextLine[k].data.size * 1/3);
			mod_sysmonitor_ReDraw();
		}


		for(i=k+1;i<this.TextLine.length;i++){
			this.TextLine[i].label.visible = false;
			this.TextLine[i].data.visible = false;
		}

		return true;
	}

	function plugin_video_ReDraw() {
		this.frame.width = mod_sysmonitor_width;

		for(var i = 0; i < this.TextLine.length ; i++) {
			this.TextLine[i].size = mod_sysmonitor_size;
			this.TextLine[i].vOffset = i * mod_sysmonitor_size;
		}

		// Re-Calculating sensore height (with background)
		plugin_video_update.call(this);

		return true;
	}

}

// --------------------------------------------------------------------------------
// The END